import { ChangeDetectionStrategy, ChangeDetectorRef, Component, OnInit, ViewChild } from '@angular/core';
import { formatSize, UploadComponent, UploadStatusEnum, UploadFileStatusEnum, UploadFile } from '@iux/live';
import { Subject } from 'rxjs';

@Component({
  selector: 'app-remove',
  templateUrl: './remove.component.html',
  styles: [],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class RemoveComponent implements OnInit {
  files;

  content = '';
  originEle: EventTarget;
  visible = false;
  loading = false;
  removeState$: Subject<boolean>;
  @ViewChild(UploadComponent, { static: true }) lvUpload: UploadComponent;

  constructor() { }

  ngOnInit() {
    this.files = [{
      key: '-1',
      name: 'xxx.png',
      size: 10000,
      fileSize: formatSize(10000),
      status: UploadFileStatusEnum.SUCCESS
    }];

    this.lvUpload.setUploadStatus(UploadStatusEnum.COMPLETE);
  }

  beforeRemove = (e: MouseEvent, file: UploadFile) => {
    this.originEle = e.currentTarget;
    this.content = `确定删除${file.name}吗？`;
    this.visible = true;
    this.removeState$?.complete();
    this.removeState$ = new Subject<boolean>();
    return this.removeState$;
  }

  confirm() {
    this.loading = true;
    setTimeout(() => {
      this.loading = false;
      this.visible = false;
      this.removeState$.next(true);
      this.removeState$.complete();
    }, 2000);
  }

  cancel() {
    this.visible = false;
    this.removeState$.next(false);
    this.removeState$.complete();
  }

  change(e) {
    console.log(e);
  }
}
